package com.hc.appanimal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.hc.DTO.LoginDTO;
import com.hc.DTO.TokenDTO;
import com.hc.constants.Constants;
import com.hc.model.utils.LoginModel;
import com.hc.model.utils.TokenModel;
import com.hc.webservice.ServiceHandler;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AsystaskLogin extends AsyncTask<Void, Void, Void> {
	final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	final ServiceHandler sh = new ServiceHandler();
	TokenDTO toke;
	LoginDTO dtoLogin;
	String err = "";
	String success = "";
	String strerr = "";
	String key = "";
	String _result = "";
	Context mContext;

	public AsystaskLogin(Context context) {
		mContext = context;
		LoginModel model = LoginModel.getInstance();
		dtoLogin = model.getUserNamePassword(mContext);

	}

	@Override
	protected Void doInBackground(Void... params) {
		nameValuePairs.add(new BasicNameValuePair("username", dtoLogin
				.getUserName()));
		nameValuePairs.add(new BasicNameValuePair("grant_type", "password"));
		nameValuePairs.add(new BasicNameValuePair("password", dtoLogin
				.getPassWord()));
		String jsonStr = sh.makeServiceCall(Constants.URL_LOGIN,
				ServiceHandler.POST, nameValuePairs);

		// String jsonStr = excutePost(Constants.URL_LOGIN,
		// "grant_type=password&username=vsj_1987@yahoo.com&password=Admin@12345");
		try {
			JSONObject object1;
			object1 = new JSONObject(jsonStr);
			for (Iterator<String> iter = object1.keys(); iter.hasNext();) {
				String key = iter.next();
				if (key.equalsIgnoreCase("error_description")) {
					strerr = object1.getString("error_description");
					_result = strerr;
				} else if (key.equalsIgnoreCase("access_token")) {
					// finish
					String access_token = object1.getString("access_token");
					_result = key;
					String token_type = object1.getString("token_type");
					String expires = object1.getString(".expires");
					toke = new TokenDTO();
					toke.setAccess_token(access_token);
					toke.setToken_type(token_type);
					toke.setExpires(expires);
					TokenModel tokenModel = TokenModel.getInstance();
					tokenModel.updateToken(toke, mContext);

					int Flag = dtoLogin.getFlag();
					Flag = Flag + 1;

					dtoLogin.setUserName(dtoLogin.getUserName());
					dtoLogin.setPassWord(dtoLogin.getPassWord());
					dtoLogin.setFlag(Flag);
					LoginModel modelLogin = LoginModel.getInstance();
					modelLogin.updateLogin(dtoLogin, mContext);
				}

			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		if (_result.equalsIgnoreCase("The user name or password is incorrect.")) {
			final Dialog dialogerr = new Dialog(mContext,
					android.R.style.Theme_Translucent);
			dialogerr.requestWindowFeature(Window.FEATURE_NO_TITLE);

			dialogerr.setCancelable(true);
			dialogerr.setContentView(R.layout.dialog_err);
			TextView txtContent = (TextView) dialogerr
					.findViewById(R.id.txtContent);
			txtContent.setText(strerr);
			Button btnOke = (Button) dialogerr.findViewById(R.id.bntOk);

			btnOke.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialogerr.dismiss();
				}
			});

			dialogerr.show();
		} else if (_result.equalsIgnoreCase("access_token")) {

		}
	}

}
